package com.cictec.middleware.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cictec.middleware.core.entity.BusLine;
import com.cictec.middleware.core.entity.BusLineMapPoint;
import com.cictec.middleware.core.entity.BusLineStation;
import com.cictec.middleware.core.entity.StandbyZone;
import com.cictec.middleware.core.entity.pojo.BaseBusLineInfo;
import com.cictec.middleware.core.service.BusLineMapPointService;
import com.cictec.middleware.core.service.BusLineService;
import com.cictec.middleware.core.service.BusLineStandbyService;
import com.cictec.middleware.core.service.BusLineStataionService;

@RestController
public class BusLineController {

	private static Logger logger = LoggerFactory.getLogger(BusController.class);

	@Autowired
	private BusLineService busLineService;
	@Autowired
	private BusLineMapPointService busLineMapPointService;
	@Autowired
	private BusLineStandbyService busLineStandbyService;
	@Autowired
	private BusLineStataionService busLineStataionService;

	
	@RequestMapping(value = "/dc/line/buslineinfo")
	public List<BaseBusLineInfo> getListBusLineInfos() {
		List<BaseBusLineInfo> list = new ArrayList<BaseBusLineInfo>();
		List<BusLine> lineList = busLineService.getListBusLine();
		for (BusLine busLine : lineList) {
			BaseBusLineInfo info = new BaseBusLineInfo();
			info.setBusLine(busLine);
			info.setBusLineIds(busLineService.selectBusLineIds(busLine.getLineUuid()));
			info.getStandbyZones().addAll(busLineService.getStandbyZones(busLine.getLineUuid()));
			info.getBusLineMapPointsMap().put("1", busLineMapPointService.getBusLineMapPoint(busLine.getLineUuid(), "1"));
			info.getBusLineMapPointsMap().put("2", busLineMapPointService.getBusLineMapPoint(busLine.getLineUuid(), "2"));
			info.getBusLineStationMap().put("1", busLineStataionService.getLineStationAll(busLine.getLineUuid(), "1"));
			info.getBusLineStationMap().put("2", busLineStataionService.getLineStationAll(busLine.getLineUuid(), "2"));
		}
		
		return list;
	}
	
	@RequestMapping(value = "/dc/line/buslineinfo/{lineUuid}")
	public BaseBusLineInfo getListBusLineInfo(@RequestParam("lineUuid") String lineUuid) {
		
		
		BaseBusLineInfo info = new BaseBusLineInfo();
		info.setBusLine(busLineService.getLineByLineUuid(lineUuid));
		info.setBusLineIds(busLineService.selectBusLineIds(lineUuid));
		info.getStandbyZones().addAll(busLineService.getStandbyZones(lineUuid));
		info.getBusLineMapPointsMap().put("1", busLineMapPointService.getBusLineMapPoint(lineUuid, "1"));
		info.getBusLineMapPointsMap().put("2", busLineMapPointService.getBusLineMapPoint(lineUuid, "2"));
		info.getBusLineStationMap().put("1", busLineStataionService.getLineStationAll(lineUuid, "1"));
		info.getBusLineStationMap().put("2", busLineStataionService.getLineStationAll(lineUuid, "2"));
		return info;
	}
	
	
	
	@RequestMapping(value = "/dc/line")
	public List<BusLine> getListBusLine() {
		List<BusLine> lineList = busLineService.getListBusLine();
		return lineList;
	}
	
	@RequestMapping(value = "/dc/line/lineUuid")
	public BusLine getListBusLine(@RequestParam("lineUuid") String lineUuid) {
		BusLine line = busLineService.getLineByLineUuid(lineUuid);
		return line;
	}
	
	@RequestMapping(value = "/dc/line/busLineMapPoint")
	public Map<String, List<BusLineMapPoint>> getBusLineMapPoint(@RequestParam("lineUuid") String lineUuid) {
		
		Map<String, List<BusLineMapPoint>> pointsMap = new HashMap<String, List<BusLineMapPoint>>();
		List<BusLineMapPoint> upPoints = busLineMapPointService.getBusLineMapPoint(lineUuid, "1");
		List<BusLineMapPoint> downPoints = busLineMapPointService.getBusLineMapPoint(lineUuid, "2");
		pointsMap.put("1", upPoints);
		pointsMap.put("2", downPoints);
		return pointsMap;
	}

	@RequestMapping(value = "/dc/line/busLineStation")
	public Map<String, List<BusLineStation>> getBusLineStation(@RequestParam("lineUuid") String lineUuid) {
		List<BusLineStation> upStations = busLineStataionService.selectLineStationByLineType(lineUuid, "1");
		List<BusLineStation> downStations = busLineStataionService.selectLineStationByLineType(lineUuid, "2");
		Map<String, List<BusLineStation>> lineStationMap = new HashMap<String, List<BusLineStation>>();
		lineStationMap.put("1", upStations);
		lineStationMap.put("2", downStations);
		
		return lineStationMap;
	}
	
	@RequestMapping(value = "/dc/line/busLineStandby")
	public List<StandbyZone> getBusLineStandby(@RequestParam("lineUuid") String lineUuid) {
		List<StandbyZone> standbyZone = busLineStandbyService.getStandbyZones(lineUuid);
		return standbyZone;
	}

	
}
